#!/bin/bash


#OneDM自动化安装脚本
#usg: ./onedm.sh OneDM.zip
#


#usage message
if [ $# != 1 ] ; then
  echo "USAGE:sh $0 OneDM Package"
  echo " e.g.:sh $0 OneDM-1.2.1-aarch64.tar.xz"
  echo "... ..."
  echo "OneDM Package must be *.tar.xz file"
  exit 1;
fi



#
PACKAGE=$1
PWD=`pwd`
PLATFORM=`uname -i`
ONEDMNAME=${PACKAGE%%"-$PLATFORM"*}
INSTALL_PATH="/u01/onedm"


#check soft package
function func_check_package() {
#  echo "******************** func_check_package ********************"
  echo -e "\e[1;32m******************** check OneDM Package ********************\e[0m"
  result=`ls $PACKAGE`
  if [[ $result = "" ]]; then
    echo -e "\e[1;31m[Input ERROR] OneDM Package Invalid\e[0m"
    echo "USAGE: $0 OneDM Package"
    echo " e.g.: $0 OneDM-1.2.1-aarch64.tar.xz"
    echo "... ..."
    echo "OneDM Package must be *.tar.xz file"
    exit 1
  fi 
  echo -e "\e[1;32m******************** check sucessfully ********************\e[0m"
}

function func_set_env() {
#  echo "******************** func_set_env ********************"
  echo -e "\e[1;32m******************** set env ********************\e[0m"
  systemctl stop firewalld 
  systemctl disable firewalld
  result=`systemctl status firewalld |grep inactive`
  if [[ $result == "" ]]; then
    echo -e "\e[1;31m[WARNNING] firewalld close err\e[0m"
  fi
  echo -e "\e[1;32m******************** env sucessfully ********************\e[0m"
}

function func_install() {
#  echo "******************** func_install ********************"
  echo -e "\e[1;32m******************** onedm install ********************\e[0m"
  #IP=""
  PORT="5000"
  USER="root"
  #PASSWD=''
  FIRSTUSER="admin@onedm.com"
  FIRSTPASSWD="admin@onedm.com"

  #IP
  echo -e "\e[1;32mThe possble IP:\e[0m"
  ip addr show | grep -E 'inet [0-9]' | grep -v '127.0.0.1' | awk '{print $2}' | awk -F '/' '{print $1}'
  echo -e "INPUT IP:"
  read IP 
  if [[ $IP == "" ]]; then
    echo -e "\e[1;31m[Input ERROR] IP INPUT ERROR\e[0m"
    exit 1
  fi

  #PASSWD
  echo -e "INPUT Password of root:"
  read PASSWD 
  if [[ $PASSWD == "" ]]; then
    echo -e "\e[1;31m[Input ERROR] Password INPUT ERROR\e[0m"
    exit 1
  fi

  mkdir -p $INSTALL_PATH
  sed -i "s/ADM_IP_ARRAY=()/ADM_IP_ARRAY=($IP)/g"                                           onedm_install.sh
  sed -i "s/ADM_WEB_PORT=80/ADM_WEB_PORT=$PORT/g"                                           onedm_install.sh
  sed -i "s/SSH_USER=root/SSH_USER=$USER/g"                                                 onedm_install.sh
  sed -i "s/SSH_PASSWORD=abc123/SSH_PASSWORD=$PASSWD/g"                                     onedm_install.sh
  sed -i "s/ADM_BASE_DIR=\/onedm/ADM_BASE_DIR=\/u01\/onedm\/$ONEDMNAME/g"                   onedm_install.sh
  sed -i "s/FIRST_SUPERUSER=adm@onesql.com/FIRST_SUPERUSER=$FIRSTUSER/g"                    onedm_install.sh
  sed -i "s/FIRST_SUPERUSER_PASSWORD=Q!w2e3r4/FIRST_SUPERUSER_PASSWORD=$FIRSTPASSWD/g"      onedm_install.sh

  sh onedm_install.sh install

  echo -e "\e[1;32m******************** install sucessfully ********************\e[0m"
  echo -e "\e[1;32mweb: http://$IP:$PORT \e[0m"
  echo -e "\e[1;32mFirst UserName:$FIRSTUSER\e[0m"
  echo -e "\e[1;32mFirst Password:$FIRSTPASSWD\e[0m"
}




func_check_package
func_set_env
func_install

